package edu.henu.personnel.service.talent;

import edu.henu.personnel.constant.ErrorConstant;
import edu.henu.personnel.dao.TalentDao;
import edu.henu.personnel.entity.Talent;
import edu.henu.personnel.entity.Teacher;
import edu.henu.personnel.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: PersonnelProject
 * @description: Talent业务实现类
 * @author: zhuyang
 * @create: 2019-04-27 11:30
 */
@Transactional
@Service
public class TalentServiceImpl implements TalentService {

    @Autowired
    TalentDao talentDao;

    @Override
    public List<Talent> getTalentList() {
        return talentDao.getAllTalentList();
    }

    @Override
    public Talent getTalentInfoById(Integer ttid) {
        if (null == ttid)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        return talentDao.getTalentInfoById(ttid);
    }

    @Override
    public Talent getTalentInfoByTname(String tname) {
        if (StringUtils.isBlank(tname)) {
            throw BusinessException.withErrorCode(ErrorConstant.Auth.USERNAME_PASSWORD_IS_EMPTY);
        }
        return talentDao.getTalentInfoByTname(tname);
    }

    @Override
    public List<Teacher> getAllTeacherInTheTalentById(Integer ttid) {
        if (null == ttid) {
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        }
        Talent talent = talentDao.getAllTeacherInTheTalentById(ttid);
        return talent.getTeachers();
    }

    @Override
    public Talent addTalentInfo(Talent talent) {
        if (null == talent) {
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        }
        int result = talentDao.insertTalentInfo(talent);
        if (result == 1) {
            return talent;
        } else {
            return null;
        }
    }

    @Override
    public int modifyTalentInfo(Talent talent) {
        if (null == talent || null ==  talent.getTtid()) {
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        }
        if (StringUtils.isBlank(talent.getTname())) {
            throw BusinessException.withErrorCode(ErrorConstant.Tal.TNMAE_IS_EMPTY);
        }
        int result = talentDao.updateTalentInfo(talent);
        return 0;
    }

    @Override
    public int removeTalentInfo(Integer ttid) {
        return 0;
    }
}
